package cn.liyicun.school.car2.controller.driver;

import java.util.Date;

import com.jfinal.aop.Clear;
import com.jfinal.plugin.activerecord.Db;

import cn.liyicun.school.car2.base.BaseDriverController;
import cn.liyicun.school.car2.base.LoginDriver;
import cn.liyicun.school.car2.interceptor.DriverLoginInterceptor;
import cn.liyicun.school.car2.model.DriverInfo;
import cn.liyicun.school.car2.model.UserInfo;

@Clear(DriverLoginInterceptor.class)
public class DriverController extends BaseDriverController {

	public void login() {
		System.out.println("--login--------");
	}

	public void login2() {

		String username = getPara("username", "").trim().toLowerCase();
		String password = getPara("password", "");
		UserInfo data = UserInfo.dao.findFirst("select * from user_info where username=?", username);

		verification(data == null, "用户或密码不对!");
		verification(data.getStatus() != 1, "用户状态不对!");
		verification(!paswordEquals(password, data), "用户或密码不对!");

		LoginDriver data1 = new LoginDriver();
		data1.setId(data.getId());
		data1.setPhone(data.getPhone());

		data1.setNickname(data.getNickname());
		DriverInfo driver = DriverInfo.dao.findById(data.getId());
		data1.setIsDriver(0);
		if (driver != null) {
			verification(driver.getStatus() != 1, "司机审批中!");
			data1.setNickname(driver.getNickname());
			data1.setIsDriver(driver.getStatus());
		} else {
			setLoginAccount(data1);
			myRenderJsonLink("需要升级为司机!", "/driver/userUpgrade");
			return;
		}

		Integer signPlace = Db.queryInt("select place_id from  driver_sign where  `status`=1 and dr_id=? limit 1",
				data.getId());
		data1.setSignPlace(signPlace == null ? 0 : signPlace);

		setLoginAccount(data1);

		// myRenderJsonLink("需要升级为司机!","/driver/userUpgrade");
		// myRenderJson("登陆成功!");
		myRenderJsonSuccess("");

	}

	public void logout() {
		logOutAccount();
		redirect301("login?" + System.currentTimeMillis(), false);
	}

	public void userUpgrade() {
		LoginDriver acc = getLoginAccount(false);

		if (acc == null) {
			redirect301("login?" + System.currentTimeMillis(), false);
			return;
		}

		setAttr("info", acc);
		System.out.println("--userUpgrade--------");
	}

	public void userUpgrade2() {
		String nickname = getPara("nickname", "");
		String license_plate = getPara("license_plate", "");
		String car_colour = getPara("car_colour", "");
		Integer seat_count = getParaToInt("seat_count", 0);
		verification(seat_count <= 0, "座位数不能对");
		verification(seat_count > 99, "座位数太大");
		LoginDriver acc = getLoginAccount();

		DriverInfo info = DriverInfo.dao.findById(acc.getId());
		if (info == null) {
			info = new DriverInfo();
			info.setCreateTime(new Date());
			info.setId(acc.getId());
			info.setNickname(nickname);
			info.setSeatCount(seat_count);
			info.setLicensePlate(license_plate);
			info.setCarColour(car_colour);
			info.setStatus(1);
			info.save();
		} else {
			info.setSeatCount(seat_count);
			info.setStatus(1);
			info.setNickname(nickname);
			info.setLicensePlate(license_plate);
			info.setCarColour(car_colour);
			info.update();
		}
		acc.setIsDriver(1);

		myRenderJsonLink("升级成功!", "/driverOrder/userOrderList");
		// redirect301("/driverOrder/userOrderList", false);
	}

	public void loginCheck() {
		System.out.println("----loginCheck------");
		if (getLoginAccount(false) == null) {
			redirect301("login", false);
		} else {
			redirect301("/driverOrder/userOrderList", false);
		}
	}

}
